<?php
	//Food class
	class Food {
		private $food_id;
		private $user_id;
		private $food_name;
		private $food_calories;
		private $food_note;
		private $food_time;
		
		//Constructor for Food
		//
		//Param: user id, food name, calories, note, time
		function __construct($user_id, $food_name, $food_calories, $food_note, $food_time = null) {
			$this->setUserID($user_id);
			$this->setName($food_name);
			$this->setCalories($food_calories);
			$this->setNote($food_note);
			$this->setTime($food_time);
		}
		
		//Setter for Food ID
		function setFoodID($val) {
			$this->food_id = $val;
		}
		//Getter for Food ID
		function getFoodID() {
			return $this->food_id;
		}
		
		//Setter for User ID
		function setUserID($val) {
			$this->user_id = $val;
		}
		//Getter for User ID
		function getUserID() {
			return $this->user_id;
		}
		
		//Setter for Food name
		function setName($val) {
			$this->food_name = $val;
		}
		//Getter for Food name
		function getName() {
			return $this->food_name;
		}
		
		//Setter for Food calories
		function setCalories($val) {
			$this->food_calories = $val;
		}
		//Getter for Food calories
		function getCalories() {
			return $this->food_calories;
		}
		
		//Setter for Food note
		function setNote($val) {
			$this->food_note = $val;
		}
		//Getter for Food note
		function getNote() {
			return $this->food_note;
		}
		
		//Setter for Food time
		function setTime($val = null) {
			if ($val === null) {
				$this->food_time = date(DATE_FORMAT);
			} else {
				$this->food_time = $val;
			}
		}					
		//Getter for Food time
		function getTime() {
			return $this->food_time;
		}
		
		//Insert new Food info into database
		//Sets Food ID to current Food
		//
		//Return: Food ID or O
		function insert() {
			$db = Database::get();
			$query = "INSERT INTO " . FOOD_TABLE . " 
				(user_id, food_name, food_calories, food_note, created_at)
				VALUES(?, ?, ?, ?, ?);";
			$stmt = $db->prepare($query);
			if ($stmt) {

				$user_id = $this->getUserID();
				$food_name = $this->getName();
				$food_calories = $this->getCalories();
				$food_note = $this->getNote();
				$food_time = $this->getTime();
				
				$stmt->bind_param("dsdss", $user_id, $food_name, $food_calories, $food_note, $food_time);
				$stmt->execute() or die('Invalid query for Food: ' . $db->error);
				$food_id = $stmt->insert_id;
				$this->setFoodID($food_id);
				$stmt->close();

				
				//Create notification for the doctor to input into database
				//Can use str_replace for arrays but too troublesome, will edit in future
				$notification_message = str_replace('PERSON', User::findName($user_id), FOOD_DOCTOR_MSG);
				$notification_message = str_replace('FOOD', $food_name, $notification_message);
				$notification_message = str_replace('CALORIES', $food_calories, $notification_message);
				$notification_type_id = Notification::findTypeID('food_doctor');

				$doctor_list = Patient::doctorList($user_id);
				if ($doctor_list !== null) {
					foreach ($doctor_list as $doctor) {			
						$doctor_id = $doctor['doctor_id'];
						if (Patient::checkDoctor($doctor_id, $user_id)) {
							$notification = new Notification($doctor_id, $notification_type_id, $notification_message, $user_id, $food_id, $food_time);
							$notification->insert();
						}
					}
				}				
				
				return intval($food_id);
				
			}
			return 0;
		}
		
		//Remove Food from database
		//
		//Return: true or false
		function remove() {
			$db = Database::get();
			$query = "DELETE FROM " . FOOD_TABLE . " 
				WHERE food_id = ?;";
			$stmt = $db->prepare($query);
			if ($stmt) {
				$food_id = $this->getFoodID();
				$stmt->bind_param("d", $food_id);
				$stmt->execute() or die('Invalid query for Food: ' . $db->error);
				$stmt->close();
				//Create notification for the doctor to delete from database
				$user_id = $this->getUserID();
				$doctor_list = Patient::doctorList($user_id);
				
				$notification_type_id = Notification::findTypeID('food_doctor');
				$notification_time = $this->getTime();
				
				if ($doctor_list !== null) {
					foreach ($doctor_list as $doctor) {			
						$doctor_id = $doctor['doctor_id'];
						if (Patient::checkDoctor($doctor_id, $user_id)) {
							$notification_id = Notification::findIDWithLog($doctor_id, $notification_type_id, $user_id, $food_id);
							$notification = Notification::createNotification($notification_id);
							$notification->remove();
						}
					}
				}							
				return true;
			}
			return false;
		}
		
		//Create Food object given the ID by taking information from the database
		//Information taken from the database: user id, name, ccalories, note, time
		//
		//Param: Food id
		//Return: Food object
		static function createFood($id) {
			$db = Database::get();
			$query = "SELECT * FROM " . FOOD_TABLE . "
					  WHERE food_id = ?;";
			$stmt = $db->prepare($query);
			if ($stmt) {
				$stmt->bind_param("d", $id);
				$stmt->execute() or die('Invalid query for Symptom: ' . $db->error);
				$result = $stmt->get_result();
				$food_row = null;
				if ($row = $result->fetch_assoc()) {
					$food_row = $row;
				} 
				$stmt->close();
			} else {
				die('Invalid query for Food: ' . $db->error);
			}
			
			$food = null;
			if ($food_row !== null) {
				$user_id = $food_row['user_id'];
				$food_name = $food_row['food_name'];
				$food_calories = $food_row['food_calories'];
				$food_note = $food_row['food_note'];
				$food_time = $food_row['created_at'];
				
				$food = new Food($user_id, $food_name, $food_calories, $food_note, $food_time);
				$food->setFoodID($id);
				}
				
				
			return $food;
		}
		
		//Get list of Food given the user id
		//
		//Param: user id
		//Return: List of Food
		static function getFoodList($user_id) {
			$db = Database::get();
			$query = "SELECT * FROM " . FOOD_TABLE . "
					  WHERE user_id = ?
					  ORDER BY created_at;";
			$stmt = $db->prepare($query);
			if ($stmt) {
				$stmt->bind_param("d", $user_id);
				$stmt->execute() or die('Invalid query for Food: ' . $db->error);
				$result = $stmt->get_result();
				$food_list = null;
				while ($row = $result->fetch_assoc()) {
					$food_list[] = $row;
				} 
				$stmt->close();
			} else {
				die('Invalid query for Food: ' . $db->error);
			}
			
			return $food_list;
		}

		//Get list of Food calories per day given the user id
		//
		//Param: user id
		//Return: List of Food calories per day		
		static function getFoodCaloriesPerDay($user_id) {
			$db = Database::get();
			$query = "SELECT DATE(created_at) as food_date, SUM(food_calories) as food_calories FROM " . FOOD_TABLE . "
					  WHERE user_id = ?
					  GROUP BY DATE(created_at);";
			$stmt = $db->prepare($query);
			if ($stmt) {
				$stmt->bind_param("d", $user_id);
				$stmt->execute() or die('Invalid query for Food: ' . $db->error);
				$result = $stmt->get_result();
				$food_list = null;
				while ($row = $result->fetch_assoc()) {
					$food_list[] = $row;
				} 
				$stmt->close();
			} else {
				die('Invalid query for Food: ' . $db->error);
			}
			
			return $food_list;
		}
		
	}
?>